<template>
  <div @mouseenter="hanldHover" @mouseleave="hanldLeave">
    <el-card>
      <div class="statistics">
        <div class="icon" ref="iconDivRef">
          <i :class="icon" ref="iconRef" :style="{'color':color}"></i>
        </div>
        <div class="data">
          <div class="title">{{title}}</div>
          <span>{{initNumber | formatNumber}}</span>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "StatisticsCard",
  props: {
    icon: {
      type: String,
      default: "el-icon-user-solid",
    },
    color: {
      type: String,
      default: "rgb(14, 158, 241)",
    },
    number: {
      type: Number,
      default: 0,
    },
    title: {
      type: String,
      default: "未知",
    },
  },
  data() {
    return {
      timer: null,
      initNumber: 0,
    };
  },
  filters: {
    formatNumber(val) {
      return val.toString().replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
    },
  },
  mounted() {
    this.countTo();
  },
  created() {},
  methods: {
    countTo() {
      this.timer = setInterval(() => {
        this.number > 10000
          ? (this.initNumber += 1000)
          : (this.initNumber += 1);
        if (this.initNumber >= this.number) {
          this.initNumber = this.number;
          clearInterval(this.timer);
        }
      }, 10);
    },
    hanldHover() {
      this.$refs.iconDivRef.style.backgroundColor = this.color;
      this.$refs.iconRef.style.color = "#fff";
    },
    hanldLeave() {
      this.$refs.iconDivRef.style.backgroundColor = "#fff";
      this.$refs.iconRef.style.color = this.color;
    },
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
};
</script>

<style scoped lang="scss">
.statistics {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
}
.statistics:hover {
  .icon {
    background-color: rgb(14, 158, 241);

    i {
      color: #fff;
    }
  }
}
.icon {
  transition: all 0.2s;
  width: 78px;
  height: 78px;
  border-radius: 6px;
  text-align: center;
  i {
    line-height: 1.3;
    font-size: 60px;
    color: rgb(14, 158, 241);
    text-align: center;
  }
}
.data {
  .title {
    font-size: 16px;
    color: rgb(49, 49, 49);
    font-weight: 600;
    margin-bottom: 4px;
  }
  span {
    font-size: 20px;
    font-weight: 800;
    letter-spacing: 2px;
  }
}
</style>
